衷于栖
  • 衷于栖
  • 首页
  • 归档
  • 关于

Image
Profile Picture

衷于栖

自由开发者

分类目录

三维技术4 介绍2 应用1 异常1 技术笔记17 游戏2 源码解读3 管理5 读书笔记3 车联网3 转载11 随笔3

热门标签

  • GIT
  • 工作流指南
  • docker
  • SCRUM
  • JT808
  • 百度地图
  • 狼人杀
  • 模型数据结构
  • 敏捷
  • 扩展
  • 学习WEBGL系列
  • 可维护
  • GlTF
  • CentOS
  • 高德地图
  • 集中式
  • 郭麒麟
  • 郭德纲
  • 进阶
  • 路由节点编辑器

微信订阅

Image

友情链接

王海达博客 Steve Yegge Debug 客栈 Codelei's Blog 笛卡尔积 Java九点半课堂 薛定喵君

【JFINAL】 JFinal升级Jetty9

2018-07-16     技术笔记


[2019-03-11更新] 新版已经更新到了9所以这篇文章就多余了。

原因就不记录了,就简单的记录一下修改的地方。 注意: 一切修改基于JFinal-3.4-SNAPSHOT源码,仅供参考

pom修改依赖

更换关于jetty的版本信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<version>9.3.23.v20180228</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-webapp</artifactId>
<version>9.3.23.v20180228</version>
<scope>provided</scope>
</dependency>
<!-- jsp support by jetty -->
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-jsp</artifactId>
<version>9.2.24.v20180105</version>
<scope>provided</scope>
</dependency>

增加服务类 (此类网络收集,未测试所有环境)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// 直接放到Jfinal同名包里即可
package com.jfinal.server;

import java.io.File;
import java.io.IOException;
import java.net.DatagramSocket;
import java.net.ServerSocket;

import org.eclipse.jetty.server.HttpConfiguration;
import org.eclipse.jetty.server.HttpConnectionFactory;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.server.SessionManager;
import org.eclipse.jetty.server.session.HashSessionManager;
import org.eclipse.jetty.server.session.SessionHandler;
import org.eclipse.jetty.webapp.WebAppContext;

import com.jfinal.core.Const;
import com.jfinal.kit.FileKit;
import com.jfinal.kit.LogKit;
import com.jfinal.kit.PathKit;
import com.jfinal.kit.StrKit;

public class Jetty9Server implements IServer{

public static void main(String[] args) {
new Jetty9Server("src/main/webapp",8080,"/").start();
}

private String webAppDir;
private int port;
private String context;
private boolean running = false;
private Server server;
private WebAppContext webApp;

public Jetty9Server(String webAppDir, int port, String context) {
if (webAppDir == null) {
throw new IllegalStateException("Invalid webAppDir of web server: " + webAppDir);
}
if (port < 0 || port > 65535) {
throw new IllegalArgumentException("Invalid port of web server: " + port);
}
if (StrKit.isBlank(context)) {
throw new IllegalStateException("Invalid context of web server: " + context);
}

this.webAppDir = webAppDir;
this.port = port;
this.context = context;
// this.scanIntervalSeconds = scanIntervalSeconds;
}

public void start() {
if (!running) {
try {
running = true;
doStart();
} catch (Exception e) {
System.err.println(e.getMessage());
LogKit.error(e.getMessage(), e);
}
}
}

public void stop() {
if (running) {
try {server.stop();} catch (Exception e) {LogKit.error(e.getMessage(), e);}
running = false;
}
}

private void doStart() {
if (!available(port)) {
throw new IllegalStateException("port: " + port + " already in use!");
}

deleteSessionData();

System.out.println("Starting JFinal " + Const.JFINAL_VERSION);
server = new Server();
HttpConfiguration http_config = new HttpConfiguration();
ServerConnector connector = new ServerConnector(server,new HttpConnectionFactory(http_config));
connector.setReuseAddress(true);
connector.setIdleTimeout(30000);
connector.setPort(port);
server.addConnector(connector);

webApp = new WebAppContext();
// 在启动过程中允许抛出异常终止启动并退出 JVM
webApp.setThrowUnavailableOnStartupException(true);
webApp.setContextPath(context);
// webApp.setWar(webAppDir);
webApp.setResourceBase(webAppDir);
webApp.setContextPath(context);
webApp.setMaxFormContentSize(81920000);
webApp.getInitParams().put("org.eclipse.jetty.servlet.Default.dirAllowed", "false");
webApp.getInitParams().put("org.eclipse.jetty.servlet.Default.useFileMappedBuffer", "true");
webApp.getInitParams().put("org.eclipse.jetty.server.Request.maxFormContentSize", "-1");
persistSession(webApp);

server.setHandler(webApp);
try {
System.out.println("Starting web server on port: " + port);
server.start();
System.out.println("Starting Complete. Welcome To The JFinal World :)");
server.join();
} catch (Exception e) {
LogKit.error(e.getMessage(), e);
System.exit(100);
}
return;
}

private void deleteSessionData() {
try {
FileKit.delete(new File(getStoreDir()));
}
catch (Exception e) {
LogKit.logNothing(e);
}
}

private String getStoreDir() {
String storeDir = PathKit.getWebRootPath() + "/../../session_data" + context;
if ("\\".equals(File.separator)) {
storeDir = storeDir.replaceAll("/", "\\\\");
}
return storeDir;
}

private void persistSession(WebAppContext webApp) {
String storeDir = getStoreDir();
SessionManager sm = webApp.getSessionHandler().getSessionManager();
if (sm instanceof HashSessionManager) {
try {
((HashSessionManager)sm).setStoreDirectory(new File(storeDir));
} catch (IOException e) {
e.printStackTrace();
}
return ;
}
HashSessionManager hsm = new HashSessionManager();
try {
hsm.setStoreDirectory(new File(storeDir));
} catch (IOException e) {
e.printStackTrace();
}
SessionHandler sh = new SessionHandler();
sh.setSessionManager(hsm);
webApp.setSessionHandler(sh);
}

private static boolean available(int port) {
if (port <= 0) {
throw new IllegalArgumentException("Invalid start port: " + port);
}

ServerSocket ss = null;
DatagramSocket ds = null;
try {
ss = new ServerSocket(port);
ss.setReuseAddress(true);
ds = new DatagramSocket(port);
ds.setReuseAddress(true);
return true;
} catch (IOException e) {
LogKit.logNothing(e);
} finally {
if (ds != null) {
ds.close();
}

if (ss != null) {
try {
ss.close();
} catch (IOException e) {
// should not be thrown, just detect port available.
LogKit.logNothing(e);
}
}
}
return false;
}
}
#JFinal #Jetty9

Copyright © 2021 zhoyq.com. All rights reserved.

京ICP备 17068495号-1